package com.jmu.edu.IMApplication.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.button.MaterialButton;
import com.google.gson.Gson;
import com.jmu.edu.IMApplication.MyApplication;
import com.jmu.edu.IMApplication.R;
import com.jmu.edu.IMApplication.bean.GestureAndFaceResult;
import com.jmu.edu.IMApplication.net.bean.UserIdentify;
import com.jmu.edu.IMApplication.utils.HttpUtil;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ReviseCustomTextItemAdapter extends RecyclerView.Adapter<ReviseCustomTextItemAdapter.MyViewHolder> {

    private static final String TAG = "reviseCustomTextAdapter";
    private GestureAndFaceResult result;

    public void setResult(GestureAndFaceResult result) {
        this.result = result;
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //这里由于布局相似，所以这个适配器和GestureAndFaceResult的适配器使用同一个布局
        return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_gesture_and_face_result, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        String customText = result.getUserIdentifyList().get(position).getIdentifyText();
        holder.customTextButton.setText(customText);
        // 长按删除这个Text
        holder.customTextButton.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                UserIdentify userIdentify = result.getUserIdentifyList().remove(position);
                notifyItemRemoved(position);
                //向服务器发送删除请求
                Map<String, String> map = new HashMap<>();
                map.put("username", MyApplication.getUser().getUserName());
                map.put("chineseName", result.getIdentify().getChineseName());
                map.put("identifyText", userIdentify.getIdentifyText());
                RequestBody requestBody = RequestBody.create(HttpUtil.JSON, new Gson().toJson(map));
                Request request = new Request.Builder().url(HttpUtil.URL_DEL_USER_IDENTIFY).post(requestBody).build();
                MyApplication.getClient().newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e(TAG, "onFailure: ", e);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        Log.d(TAG, "onResponse: " + response.body().string());
                    }
                });
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return result.getUserIdentifyList().size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        private MaterialButton customTextButton;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            customTextButton = itemView.findViewById(R.id.item_gesture_and_face_result);
        }
    }
}
